package org.ztest.spring.di;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.context.annotation.PropertySource;
import org.springframework.context.annotation.Scope;
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
import org.springframework.core.env.Environment;
import org.ztest.dataparser.Test;

@Configuration
// 指定配置文件路径
@PropertySource("classpath:app.properties")
@ComponentScan("org.ztest.spring")
// 启动自动代理
@EnableAspectJAutoProxy
public class AppConfig
{
    // 使用指定配置文件中的配置给字段赋值
    // 可以利用这个实现配置文件到类的映射
    @Value("${fieldB}")
    int beanName;
    @Autowired
    Environment env;
    
    @Bean
    // prototype为原型模式，每次调用{@code app.getBean都将产生一个不同的bean}
    @Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
    public Test myTest()
    {
        System.out.println("Create test");
        return new Test();
    }
    
    // 要想达到@Value与properties配置文件结合使用的目的，必须静态提供这个bean
    @Bean
    public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer()
    {
        System.out.println("Create propertysourcesplaceholderconfigurer");
        return new PropertySourcesPlaceholderConfigurer();
    }
}
